<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            margin: 50px auto;
            position: relative;
        }

        #box {
            width: 100px;
            height: 100px;
            background: #000;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="container">
        <div id="box"></div>
    </div>

    <script>
        const box = document.getElementById('box')

        document.addEventListener("keydown", function (e) {
            let speed = e.shiftKey ? 20 : 5;

            if (e.keyCode === 37 && box.offsetLeft > 0) {//左
                box.style.left = box.offsetLeft - speed + 'px';
            } else if (e.keyCode === 38 && box.offsetTop > 0) {//上
                box.style.top = box.offsetTop - speed + 'px';
            } else if (e.keyCode === 39 && box.offsetLeft < 400) {//右
                box.style.left = box.offsetLeft + speed + 'px';
            } else if (e.keyCode === 40 && box.offsetTop < 400) {//下
                box.style.top = box.offsetTop + speed + 'px';
            }
        })
    </script>
</body>

</html>